package com.example.demo.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.util.List;

@Data
public class RPage<T> {
    private Integer pageNum;
    private Integer pageSize;
    private Integer total;
    private List<T> list;
    private Boolean ifFirst;
    private Boolean ifLast;

    public static <T> RPage<T> of(Page<T> page) {
        RPage<T> rPage = new RPage<>();
        int pageNum1 = (int) page.getPages();
        int pageSize1 = (int) page.getSize();
        int total1 = (int) page.getTotal();
        rPage.setPageNum(pageNum1);
        rPage.setPageSize(pageSize1);
        rPage.setTotal(total1);
        rPage.setList(page.getRecords());
        rPage.setIfFirst(pageNum1 == 1);
        rPage.setIfLast(pageNum1 == Math.ceil((double) total1 / pageSize1));
        return rPage;
    }

    public static <T> RPage<T> of(IPage<T> page) {
        RPage<T> rPage = new RPage<>();
        int pageNum1 = (int) page.getPages();
        int pageSize1 = (int) page.getSize();
        int total1 = (int) page.getTotal();
        rPage.setPageNum(pageNum1);
        rPage.setPageSize(pageSize1);
        rPage.setTotal(total1);
        rPage.setList(page.getRecords());
        rPage.setIfFirst(pageNum1 == 1);
        rPage.setIfLast(pageNum1 == Math.ceil((double) total1 / pageSize1));
        return rPage;
    }
}
